package controller;

import db.PhoneDB;
import entity.Phone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

//商品列表
@WebServlet("/phoneList")
public class ListPhoneServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        List<Phone> allPhones = PhoneDB.getAll();
        if (allPhones != null && allPhones.size() > 0) {
            for (int i = 0; i < allPhones.size(); i++) {
                Phone ph = allPhones.get(i);
                out.println("品牌 ：" + ph.getBrand() + "&nbsp;&nbsp; 价格：" +
                        ph.getPrice() + "型号&nbsp;&nbsp;" + ph.getModel() +
                        "<a href=\"cartPhoneServlet?id=" + ph.getId() +
                        " \">&nbsp;&nbsp;加入购物车 </a> <br> ");
            }
        }
    }
}
